//
//  ViewController.m
//  TestRac
//
//  Created by yidao171010 on 2020/5/14.
//  Copyright © 2020 yidao171010. All rights reserved.
//

#import "ViewController.h"
#import <ReactiveCocoa.h>
@interface ViewController ()
@property (nonatomic , copy)NSString *text1;
@property (nonatomic , copy) NSString *text2;
@property (nonatomic , strong) UITextField *textField;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self test2];
    
    
    
    
}

- (void)test1 {
    // 1.创建信号
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        // 3.发送信号
        [subscriber sendNext:@"ws"];
        // 4.取消信号，如果信号想要被取消，就必须返回一个RACDisposable
        // 信号什么时候被取消：1.自动取消，当一个信号的订阅者被销毁的时候机会自动取消订阅，2.手动取消，
        //block什么时候调用：一旦一个信号被取消订阅就会调用
        //block作用：当信号被取消时用于清空一些资源
        return [RACDisposable disposableWithBlock:^{
            NSLog(@"取消订阅");
        }];
    }];
    // 2. 订阅信号
    //subscribeNext
    // 把nextBlock保存到订阅者里面
    // 只要订阅信号就会返回一个取消订阅信号的类
    RACDisposable *disposable = [signal subscribeNext:^(id x) {
        // block的调用时刻：只要信号内部发出数据就会调用这个block
        NSLog(@"======%@", x);
    }];
    // 取消订阅
    [disposable dispose];
}

- (void)test2 {
//    RACSubject *subject = [RACSubject subject];
//    [subject subscribeNext:^(id x) {
//        //当有数据发出的时候调用
//        NSLog(@"======%@", x);
//    }];
//    [subject sendNext:@"2"];
    
    RACSignal *signal = [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        [subscriber sendNext:@"2"];
//        [subscriber sendError:nil];
        NSLog(@"subscribe block=====");
        return [RACDisposable disposableWithBlock:^{
           NSLog(@"取消订阅");
        }];
    }];
    [signal subscribeNext:^(id x) {
        NSLog(@"======%@", x);
    }];
    [signal subscribeError:^(NSError *error) {
        NSLog(@"error======%@", error);
    }];
}

//普通做法
- (void)test3 {
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        NSLog(@"input = %@",input); // input 为执行命令传进来的参数
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [subscriber sendNext:@"执行命令产生的数据"];
            return nil;
        }];
    }];
    
    RACSignal *signal = [command execute:@2];
    [signal subscribeNext:^(id x) {
        NSLog(@"x = %@",x);
    }];
    
}

//一般做法
- (void)test4 {
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        NSLog(@"input = %@",input);
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [subscriber sendNext:@4];
            return nil;
        }];
    }];
    
    [command.executionSignals subscribeNext:^(RACSignal *signal) {
        [signal subscribeNext:^(id x) {
            NSLog(@"x= %@", x);
        }];
    }];
    
    [command execute:@5];
}

//高级做法
- (void) test5{
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            
            [subscriber sendNext:@5];
            return nil;
        }];
    }];
    [command.executionSignals.switchToLatest subscribeNext:^(id x) {
        NSLog(@"%@", x);
    }];
    [command execute:@5];
}

- (void) test6{
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        NSLog(@"input = %@",input);
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [subscriber sendNext:@6];
            return nil;
        }];
    }];
    
    [command.executionSignals subscribeNext:^(id x) {
        NSLog(@"%@", x);
        [(RACSignal *)x subscribeNext:^(id x) {
            NSLog(@"x2 = %@", x);
        }];
    }];
    [command execute:@7];
}

- (void) test7{
    //  https://www.jianshu.com/p/1a0185782d8a 模拟网络请求登录
    RACCommand *command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        NSLog(@"input = %@",input);
        return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
            [subscriber sendNext:@7];
            return nil;
        }];
    }];
    [command.executionSignals.switchToLatest subscribeNext:^(id x) {
        //网络请求成功,ui层回调
    }];
    UIButton *btn = [UIButton new];
    btn.rac_command = command;
    
    UITextField *field = [UITextField new];
    RACSignal *signal_field = field.rac_textSignal;
    UITextField *field2 = [UITextField new];
    RACSignal *signal_field2 = field2.rac_textSignal;
    //组合信号
    RACSignal *comSignal = [RACSignal combineLatest:@[signal_field , signal_field2] reduce:^id(NSString *text1 , NSString *text2){
        return @(text1.length && text2.length);
    }];
    //按钮可点击方式一
    RAC(btn , enabled) = comSignal;
    //按钮可点击方式二
    [comSignal subscribeNext:^(id x) {
        btn.enabled = [x boolValue];
    }];
}

//NetTool
- (RACSignal *)loginWithUserName:(NSString *)userName pwd:(NSString *)pwd {
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3*NSEC_PER_SEC), dispatch_get_main_queue(), ^{
            [subscriber sendNext:@"login success!"];
            [subscriber sendCompleted];
        });
        return nil;
    }];
}

//ViewModel
-(void)loadCommand {
    UITextField *field1 = [UITextField new];
    UITextField *field2 = [UITextField new];
    UIButton *loginBtn = [UIButton new];
    RACSignal *signal1 = [RACObserve(self, text1) map:^id(NSString *value) {
        if (value.length > 6) {
            return @(YES);
        }
        return @(NO);
    }];
    RACSignal *signal2 = [RACObserve(self, text2) map:^id(NSString *value) {
        if (value.length > 6) {
            return @(YES);
        }
        return @(NO);
    }];
    
    RAC(self, text1) = field1.rac_textSignal;
    RAC(self, text2) = field2.rac_textSignal;
    RACSignal *comSignal = [RACSignal combineLatest:@[signal1 , signal2] reduce:^id(NSNumber *text1BoolValue, NSNumber *text2BoolValue){
        return @([text1BoolValue boolValue] && [text2BoolValue boolValue]);
    }];
    loginBtn.rac_command = [[RACCommand alloc] initWithEnabled:comSignal signalBlock:^RACSignal *(id input) {
        return [self loginWithUserName:@"tom" pwd:@"123"];
    }];
    //第一种写法
    [loginBtn.rac_command.executionSignals.switchToLatest subscribeNext:^(id x) {
        NSLog(@"login success!");
    }];
    //第二种写法,适合加载loading
    [loginBtn.rac_command.executionSignals subscribeNext:^(RACSignal *signal) {
        NSLog(@"show Loading!");
        [signal subscribeNext:^(id x) {
            NSLog(@"login success!");
            NSLog(@"hide Loading!");
        }];
    }];
}

- (void)getTuple:(RACTuple *)tuple
{
    UILabel *label = [UILabel new];
    RACTupleUnpack(NSURL *logoURL,NSString *receiverName,NSString *address) = tuple;
    label.text = logoURL.absoluteString;
    label.text = receiverName;
    label.text = [NSString stringWithFormat:@"收货地址: %@",address];
}

- (RACTuple *)setTuple {
    NSURL *logoURL = [NSURL URLWithString:@""];
    NSString *receiverName = @"";
    NSString *address = @"";
    return RACTuplePack(logoURL, receiverName, address);
}

- (void)testBtnClick{
    UIButton *btn = [UIButton new];
    btn.rac_command = [[RACCommand alloc] initWithSignalBlock:^RACSignal *(id input) {
        //do something
        return [RACSignal empty];
    }];
    
    [[[btn rac_signalForControlEvents:UIControlEventTouchUpInside] deliverOnMainThread] subscribeNext:^(id x) {
        //do something
    }];
    
    RACSignal *signal;
    [signal subscribeNext:^(id x) {
        
    }];
}

- (void)bindText{
    UITextField *textField = [UITextField new];
    _textField = textField;
    RAC(self, text1) = RACObserve(self, textField);
    RAC(self, text1) = self.textField.rac_textSignal;
    
    [RACObserve(self, textField) subscribeNext:^(id x) {
        self.text1 = x;
    }];
    [self.textField.rac_textSignal subscribeNext:^(id x) {
        self.text1 = x;
    }];
}
@end
